package com.qdsg.modulesihealsdk.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.media.FaceDetector;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by lenovo on 2018/4/7.
 */

public class CustomSurfaceView extends SurfaceView {
    private Paint facePaint;
    private SurfaceHolder mSurfaceHolder;
    private SurfaceHolder.Callback callback;

    public CustomSurfaceView(Context context) {
        super(context);

/*        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setOutlineProvider(new TextureVideoViewOutlineProvider(240));
            setClipToOutline(true);
        }*/

        initPaint();

        mSurfaceHolder = getHolder();
        // 设置 Surface 格式
        // 参数： PixelFormat中定义的 int 值 ,详细参见 PixelFormat.java
        mSurfaceHolder.setFormat(PixelFormat.TRANSPARENT);
        // 如果需要，保持屏幕常亮
        mSurfaceHolder.setKeepScreenOn(true);
    }

    public CustomSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
/*        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setOutlineProvider(new TextureVideoViewOutlineProvider(240));
            setClipToOutline(true);
        }*/

        initPaint();

        mSurfaceHolder = getHolder();
        // 设置 Surface 格式
        // 参数： PixelFormat中定义的 int 值 ,详细参见 PixelFormat.java
        mSurfaceHolder.setFormat(PixelFormat.TRANSPARENT);
        // 如果需要，保持屏幕常亮
        mSurfaceHolder.setKeepScreenOn(true);

    }

    public CustomSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

/*        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setOutlineProvider(new TextureVideoViewOutlineProvider(240));
            setClipToOutline(true);
        }*/

        initPaint();

        mSurfaceHolder = getHolder();
        // 设置 Surface 格式
        // 参数： PixelFormat中定义的 int 值 ,详细参见 PixelFormat.java
        mSurfaceHolder.setFormat(PixelFormat.TRANSPARENT);
        // 如果需要，保持屏幕常亮
        mSurfaceHolder.setKeepScreenOn(true);


    }

    public void setCallback(SurfaceHolder.Callback callback) {
        if (this.callback == null) {
            this.callback = callback;
            mSurfaceHolder.addCallback(callback);
        }
    }

    public void setCallBackNull(){
        callback = null;
    }

    private void initPaint() {
        facePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//		int color = Color.rgb(0, 150, 255);
        int color = Color.rgb(98, 212, 68);
        facePaint.setColor(color);
        facePaint.setStyle(Paint.Style.STROKE);
        facePaint.setStrokeWidth(5f);
        facePaint.setAlpha(180);
    }

    public void drawFaces(int faceNum, FaceDetector.Face faces[]) {
        if (mSurfaceHolder == null) {
            return;
        }
        Canvas mCanvas = mSurfaceHolder.lockCanvas();
        if (mCanvas == null) {
            return;
        }
        mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        for (FaceDetector.Face face : faces) {
            if (face == null) {
                break;
            }
            //mCanvas.drawBitmap(MainApplication.mRotaBitmap, 0, 0 ,paint);
            PointF pointF = new PointF();
            face.getMidPoint(pointF);
            float eyesDistance = face.eyesDistance();

            //Logger.v("onResult"+ pointF.x + "_" + pointF.y);
            mCanvas.drawRect(new Rect((int) (pointF.x - eyesDistance) * 2,
                    (int) (pointF.y - eyesDistance) * 2,
                    (int) (pointF.x + eyesDistance) * 2,
                    (int) (pointF.y + eyesDistance) * 2), facePaint);//绘制矩形
        }
        mSurfaceHolder.unlockCanvasAndPost(mCanvas);
    }

//    @Override
//    public void draw(Canvas canvas) {
//
//        Path path = new Path();
//        //用矩形表示SurfaceView宽高
//        RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
//        //50.0f即是圆角半径
//        path.addRoundRect(rect, 10.0f, 10.0f, Path.Direction.CCW);
//        //裁剪画布，并设置其填充方式
//        canvas.clipPath(path, Region.Op.REPLACE);
//        super.draw(canvas);
//    }

}
